package com.cykj.controller;

import com.cykj.dto.ResponseDto;
import com.cykj.pojo.PsUser;
import com.cykj.service.UserService;
import com.cykj.util.JwtUtils;
import com.cykj.util.MD5Util;
import com.cykj.vo.RegVo;
import com.cykj.vo.UserLoginVo;
import com.cykj.vo.UserVo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @BelongsProject: Psychology
 * @BelongsPackage: com.cykj.controller
 * @Author: 小林
 * @CreateTime: 2025-02-10 09:09
 * @Description: 用户表控制层
 * @Version: 1.0
 */
@RestController
@RequestMapping("/fuser")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param httpSession
     * @param vo
     * @return
     */
    @RequestMapping("/login")
    public ResponseDto login(HttpSession httpSession, @RequestBody UserLoginVo vo){
        String code =(String) httpSession.getAttribute("code");
        if(vo.getCode().equals(code)){
            return userService.login(vo.getAccount(), vo.getPwd(), vo.getCode());
        }else {
            return ResponseDto.error("验证码出错");
        }
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping("/reg")
    public ResponseDto reg(@RequestBody PsUser user){
        user.setUserPassword(MD5Util.encrypt(user.getUserPassword()));
        return userService.reg(user);
    }

    /**
     * 解析token
     * @param token
     * @return
     */
    @RequestMapping("/getInformationByToken")
    public ResponseDto getInformationByToken(String token){
        Claims claims = JwtUtils.parseJWT(token);
        System.out.println(claims);
        return ResponseDto.success(claims);
    }

    /**
     * 充值
     * @param vo
     * @return
     */
    @RequestMapping("/recharge")
    public ResponseDto recharge(@RequestBody UserVo vo){
        return userService.recharge(vo);
    }

    /**
     * 查询用户对应的余额
     * @param vo
     * @return
     */
    @RequestMapping("/selectUserBalance")
    public ResponseDto selectUserBalance(@RequestBody UserVo vo){
        return userService.selectUserBalance(vo);
    }

    /**
     * 扣费
     * @param vo
     * @return
     */
    @RequestMapping("/deductions")
    public ResponseDto deductions(@RequestBody UserVo vo){
        return userService.deductions(vo);
    }

    /**
     * 咨询师加钱
     * @param vo
     * @return
     */
    @RequestMapping("/addMoney")
    public ResponseDto addMoney(@RequestBody UserVo vo){
        return userService.addMoney(vo);
    }
}

